<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admins;
use App\Models\Users;
use Illuminate\Http\Request;

class UserController extends Controller {

    public function index(Request $request){
        $params = [
            'kwd'  => (string)$request->get('kwd',''),
        ];
        $lists = Users::where(function ($query) use ($params) {
                if($params['kwd']) {
                    $query ->where('username','like','%' . $params['kwd'] . '%')->orWhere('name','like','%' . $params['kwd'] . '%');
                }
            })->orderBy('created_at', 'desc')->where('id','>',1)
            ->paginate(10);
        return $this->writeJson(0,$lists);
    }

    public function update(Request $request)
    {
        $data = $request->all();
        $info = Users::findOrFail($data['id']);
        $info->update($data);
        return $this->writeJson(0,null,'修改信息成功');
    }


    //删除
    public function destroy(Request $request){
        $id = $request->get('id');
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        if(is_array($id)){
            Users::whereIn('id',$id)->delete();
        }else{
            Users::destroy($id);
        }
        return $this->writeJson(0);
    }


    public function column(Request $request){
        $params = [
            'kwd'  => (string)$request->get('kwd',''),
        ];
        $lists = Users::where(function ($query) use ($params) {
            if($params['kwd']) {
                $query ->where('truename','like','%' . $params['kwd'] . '%')
                    ->orWhere('mobile','like','%' . $params['kwd'] . '%')
                    ->orWhere('card','like','%' . $params['kwd'] . '%');
            }
            $query ->where('column','>',Users::COLUMN_0)->where('id','>',1);
        })->orderBy('created_at', 'desc')
            ->paginate(10);
        $this->stateToText($lists,[
            'column' => Users::COLUMN_TEXT,
            'position' => Users::POSITION_TEXT,
        ]);
        return $this->writeJson(0,$lists);
    }

    public function columnAll(){
        $lists = Users::where('column','>',Users::COLUMN_0)->orderBy('created_at', 'desc')
            ->get();
        return $this->writeJson(0,$lists);
    }


    public function verify(Request $request){
        $id = $request->get('id');
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        if(is_array($id)){
            Users::whereIn('id',$id)->update(['column'=>Users::COLUMN_2]);
        }else{
            Users::where('id',$id)->update(['column'=>Users::COLUMN_2]);
        }
        return $this->writeJson(0);
    }

    public function refuse(Request $request){
        $id = $request->get('id');
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        if(is_array($id)){
            Users::whereIn('id',$id)->update(['column'=>Users::COLUMN_0]);
        }else{
            Users::where('id',$id)->update(['column'=>Users::COLUMN_0]);
        }
        return $this->writeJson(0);
    }

}
